interactive login on perl cgi script- updated question [closed]

Posted by raindrop18 on Stack Overflow See other posts from Stack Overflow or by raindrop18
Published on 2010-06-04T16:03:06Z Indexed on 2010/06/07 22:12 UTC
Read the original article Hit count: 194

Filed under:

I am new for perl-cgi script. and my objective is to create user/pass interactive script to log and get information from multiple device at once. instead of add the user/pass credential on the script itself. since i am new if some one show me how to write the interactive part of the script. thanks much!!!

here is the current code

usr/local/bin/perl -wl
use CGI ':standard';
use Net::Telnet::Cisco;
# ### set the error fields to nulls
$selerror = "";
# ### Input from the screen - make sure data was input                                      
if (param() and param('Switches') ne "" and  
   (param('Mac') ne "" or param('Interface Description') ne "" or param('VLAN') ne "" )) {
# ### Put the input devices into an array.
       @devices = param('Switches');
# ### format the header data         
       print header();
       print start_html(-title=>"ShowSwitches",-BGCOLOR=>'aqua');
       print "\n",h1("<CENTER>Show Switches</CENTER>");
       print "\n",hr(),"\n";
# ### Go thru the device array.      
    foreach(@devices) {
       $error_msg   = "";
       $TAC_login_error  = "";
       $open_error  = "";
       $retry_open_error  = "";
       $prompt_error  = "";
       $password_error  = "";
# ### Take input host and use to send to Telnet
       $host = $_;
       $session = Net::Telnet::Cisco->new(Errmode => 'return',
                                          Timeout => 30);
# ### Connect to the host 
       $session->open(Host =>"$host", Timeout => 15);
       $open_error = $session->errmsg;
# ### Login with TACACS if host can be connected to
       if ($open_error eq "") {
           $session->login('USER', 'PASS');
           $TAC_login_error = $session->errmsg;
# ### Login with TACACS failed - try standard login
           if ($TAC_login_error ne "") {
# ### Connect to host
               $session->open(Host =>"$host", Timeout => 15);
               $retry_open_error = $session->errmsg;
# ### Wait for password prompt - multiple matches - devices may have different device prompts.
               if ($retry_open_error eq "") {
                   $session->waitfor(Match => '/Password:.*$/',
                                 Match => '/Enter password:.*$/',
                                 Timeout => 20);
                   $prompt_error = $session->errmsg;
                   if ($prompt_error eq "") {
# ### Input password
                      $session->print('getmeout');
                      $password_error = $session->errmsg;
                      $session->waitfor('/.*>$/');
                      $password_error = $session->errmsg;
                      }
              }
           }
       }
# ### No errors, then issue "show commands".
       if ($open_error eq "" and ($TAC_login_error eq "" or $retry_open_error eq "")
          and $prompt_error eq "" and $password_error eq "") 
          {
# ### Show Mac                             
           if (param('Mac')) {
               $cmd = 'sh mac';
               @output  = $session->cmd("$cmd");
               $show_error = "";
               $show_error = $session->errmsg;
               print "\n",h2($host . ' - ' . $cmd);
               if ($show_error ne "") {
                   $error_msg = 'Error for show mac - ' . $show_error; 
                   print b($error_msg),(br);
                   print hr(),"\n";
                   $error_msg = '';

               }
               else {
                   print pre(@output);
                   print hr(),"\n";
               }
           }
# ### Show Interface Description           
           if (param('Interface Description')) {
               $cmd = 'sh interface description';
               @output  = $session->cmd("$cmd");
               $show_error = "";
               $show_error = $session->errmsg;
               print "\n",h2($host . ' - ' . $cmd);
               if ($show_error ne "") {
                   $error_msg = 'Error for show mac - ' . $show_error; 
                   print b($error_msg),(br);
                   print hr(),"\n";
                   $error_msg = '';

               }
               else {
                   print pre(@output);
                   print hr(),"\n";
               }
           }

# ### Show VLAN                           
           if (param('VLAN')) {
               $cmd = 'sh vlan';
               @output  = $session->cmd("$cmd");
               $show_error = "";
               $show_error = $session->errmsg;
               print "\n",h2($host . ' - ' . $cmd);
               if ($show_error ne "") {
                   $error_msg = 'Error for show vlan - ' . $show_error; 
                   print b($error_msg),(br);
                   print hr(),"\n";
                   $error_msg = '';
               }
               else {
                   print pre(@output);
                   print hr(),"\n";
               }
           }
       }
       elsif ($TAC_login_error ne "" and $password_error ne "") {
           $error_msg = "Error - $host " . $TAC_login_error . ' - possible incorrect TACACS or standard password parameters on device.'; 

       }
       elsif ($open_error ne "") {
           $error_msg = "Error - $host " . $open_error . ' - cannot connect to host - is it down??'; 
       }
       elsif ($prompt_error ne "") {
           $error_msg = "Error - $host " . $prompt_error . ' - password prompt not recognized - invalid TACACS (or user) password.'; 
       }
       elsif ($password_error ne "") {
           $error_msg = "Error - $host " . $password_error . ' - possible incorrect user/password parameters on device.'; 
       }
       if ($error_msg ne "" ) {
          print b($error_msg),(br);
          print hr(),"\n";
       }
       print hr(),"\n";
       print end_html(),"\n";
   }
}
else {
# ###  No Show command was selected.
  if (param('Submit') and param('Mac') eq "" and param('Interface Description') eq "" and param('VLAN') eq "" ) {
     $selerror = 'No Show Displays were selected. Try again please!!';
  }
  elsif
# ###  No switch was selected.
     (param('Submit') and param('Switches') eq "") {
     $selerror = 'No devices were selected. Try again please!!';
  }
# ### This formats the initial Show Web page. 
  print header(-Pragma=>'no-cache'),

        start_html(-title=>"Show Displays",-BGCOLOR=>'aqua'),

        h1("<CENTER>Show Switches</CENTER>"),hr(),

        start_form(),

        b("Select Show Commands:"), 

        br(),

        br(),

        checkbox(-name=>'Mac'),

        checkbox(-name=>'Interface Description'),

        checkbox(-name=>'VLAN'),

        br(),br(),hr(),br(),

        b("Select One or More Devices:"), 

        br(),

        br(),

        scrolling_list    (-name   => 'Switches',

                           -default=> "NONE",

                           -values => ['cs6a',  'cs7a',  'cs7b', 

                                       'cs8b',  'cs9a',  'c9b',  'csa'

                                       ], 

                           -multiple => 'true',

                           -size => 7,

                             ),
                p(submit('Submit'),reset('Reset')),

        b($selerror),

        end_form(),hr(),

        end_html();

}

#

© Stack Overflow or respective owner

Related posts about perl